package com.yaorange.web.controller.nongit;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.yaorange.common.annotation.Log;
import com.yaorange.common.core.controller.BaseController;
import com.yaorange.common.core.domain.AjaxResult;
import com.yaorange.common.enums.BusinessType;
import com.yaorange.nongit.domain.NjtArea;
import com.yaorange.nongit.service.INjtAreaService;
import com.yaorange.common.utils.poi.ExcelUtil;
import com.yaorange.common.core.page.TableDataInfo;

/**
 * 地区层级关系Controller
 * 
 * @author lzh
 * @date 2025-02-27
 */
@RestController
@RequestMapping("/nongit/area")
public class NjtAreaController extends BaseController
{
    @Autowired
    private INjtAreaService njtAreaService;


    /**
     * 根据⽗节点code获取⼦节点，为null时返回所有根节点
     *
     * @param parentCode
     * @return
     */
    @ApiOperation("根据⽗节点code获取⼦节点")
    @GetMapping("/listByParentCode")
    public AjaxResult listByParentCode(@RequestParam(value = "parentCode", required = false) String parentCode) {
        List<NjtArea> list = njtAreaService.getByParentCode(parentCode);
        return AjaxResult.success(list);
    }

    /**
     * 查询地区层级关系列表
     */
    @PreAuthorize("@ss.hasPermi('nongit:area:list')")
    @GetMapping("/list")
    public TableDataInfo list(NjtArea njtArea)
    {
        startPage();
        List<NjtArea> list = njtAreaService.selectNjtAreaList(njtArea);
        return getDataTable(list);
    }

    /**
     * 导出地区层级关系列表
     */
    @PreAuthorize("@ss.hasPermi('nongit:area:export')")
    @Log(title = "地区层级关系", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NjtArea njtArea)
    {
        List<NjtArea> list = njtAreaService.selectNjtAreaList(njtArea);
        ExcelUtil<NjtArea> util = new ExcelUtil<NjtArea>(NjtArea.class);
        util.exportExcel(response, list, "地区层级关系数据");
    }

    /**
     * 获取地区层级关系详细信息
     */
    @PreAuthorize("@ss.hasPermi('nongit:area:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(njtAreaService.selectNjtAreaById(id));
    }

    /**
     * 新增地区层级关系
     */
    @PreAuthorize("@ss.hasPermi('nongit:area:add')")
    @Log(title = "地区层级关系", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NjtArea njtArea)
    {
        return toAjax(njtAreaService.insertNjtArea(njtArea));
    }

    /**
     * 修改地区层级关系
     */
    @PreAuthorize("@ss.hasPermi('nongit:area:edit')")
    @Log(title = "地区层级关系", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NjtArea njtArea)
    {
        return toAjax(njtAreaService.updateNjtArea(njtArea));
    }

    /**
     * 删除地区层级关系
     */
    @PreAuthorize("@ss.hasPermi('nongit:area:remove')")
    @Log(title = "地区层级关系", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(njtAreaService.deleteNjtAreaByIds(ids));
    }
}
